package com.cai.quartz.schedule;

import com.cai.quartz.job.MyListenerJob;
import com.cai.quartz.listener.job.MyJobListener1;
import com.cai.quartz.listener.trigger.MyTriggerListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.EverythingMatcher;
import org.quartz.impl.matchers.KeyMatcher;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class MyListenerScheduler2 {
    public MyListenerScheduler2() throws SchedulerException, InterruptedException {
        doSomeThing();
    }

    public void doSomeThing() throws SchedulerException, InterruptedException {
        Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();

        JobDetail jobDetail = JobBuilder
                .newJob(MyListenerJob.class)
                .withIdentity("job1")
                .build();
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("trigger1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(3))
                .build();

        scheduler.scheduleJob(jobDetail,trigger);

        //创建全局Trigger Listener
        //scheduler.getListenerManager()
        //                .addTriggerListener(new MyTriggerListener(), EverythingMatcher.allTriggers());

        //创建注册局部Trigger Listener 监听指定的Trigger
        scheduler
                .getListenerManager()
                .addTriggerListener(new MyTriggerListener(), KeyMatcher.keyEquals(TriggerKey.triggerKey("trigger1")));

        scheduler.start();
    }
}
